package bintree.compare;
//Comparable接口   比较器的实现 Comparator方法

import java.util.Arrays;
import java.util.Comparator;

// 此时这个类是根据年龄越大就越大进行比较
class StudentSec implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}

//此时又是根据年龄越小反而越大进行比较
class StudentDesc implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o2.getAge() - o1.getAge();
    }
}


public class Student {

    private int age;
    private String name;

    //构造方法
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    //    @Override//需要覆写Comparable里面的 compareTo方法
//    public int compareTo(Student o) {
//        //比如说我们现在自定义认为，年龄越大，这个Student则越大
//
//        //return this.age - o.age;//排序后是升序结果
//        return o.age - this.age;//排序后是降序结果
//    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Student[] students = new Student[]{
                new Student(19,"小明"),
                new Student(17,"小红"),
                new Student(18,"小刚"),
        };
        //快速排序
        //此时的Student这个类不具备可比较的能力
        // 但现在却可以传入一个该类的比较强对象
        Arrays.sort(students, new StudentSec());
        System.out.println(Arrays.toString(students));

        System.out.println("*--------分割线--------*");

        Arrays.sort(students, new StudentDesc());
        System.out.println(Arrays.toString(students));

    }

}
